﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Net_SCADA;

namespace WZXC_GBL_QUERY
{
    public partial class Form_QueryAlarmByBox : Form
    {
        string[] his_AlarmTitle=new string[5]{ "序号","警报名称", "警报等级","发生时间" ,"消除时间"};
        private Size ScreenSize;
        private DateTime sTime;
        private DateTime eTime;
        List<KeyTime> keytimes;

        public Form_QueryAlarmByBox(DateTime stime,DateTime etime, List<KeyTime> keytime)
        {
            InitializeComponent();
            sTime = stime;
            eTime = etime;
            keytimes = keytime;
        }

        private void ShowBoxAlarm()
        {
            lbl_Title.Text = "报警信息检索中......";
            LoadAlarmInterface();
            DataTable dt = DBOperate.his_GetAlarmValues(sTime,eTime);
            List<DataRow> dr=new List<DataRow>();
            IEqualityComparer<DataRow> comparer = DataRowComparer.Default;
            DataRow[] preDatarow;
            DataRow[] currentDatarow;
            DataRow[] nextDatarow;
            StringBuilder sbSelect=new StringBuilder();
            StringBuilder sbAlarmid=new StringBuilder();
            if (dt != null)
            {
                for (int i = 0 ; i < keytimes.Count ; i++)
                {
                    for (int j = 0 ; j < keytimes[i].Alarmid.Count ; j++)
                    {
                        sbSelect.Clear().Append("savetime <'").Append(keytimes[i].MoveInTime.ToString("yyyy-MM-dd HH:mm:ss")).Append("'")
                            .Append(" and alarmid='").Append(keytimes[i].Alarmid[j]).Append("'");// and AlarmActive='1'");
                        preDatarow = dt.Select(sbSelect.ToString(),"savetime desc");
                        sbSelect.Clear().Append("savetime >='").Append(keytimes[i].MoveInTime.ToString("yyyy-MM-dd HH:mm:ss")).Append("'")
                            .Append(" and savetime<='").Append(keytimes[i].MoveOutTime.ToString("yyyy-MM-dd HH:mm:ss")).Append("'")
                            .Append(" and alarmid='").Append(keytimes[i].Alarmid[j]).Append("'");
                        currentDatarow = dt.Select(sbSelect.ToString());
                        sbSelect.Clear().Append("savetime >'").Append(keytimes[i].MoveOutTime.ToString("yyyy-MM-dd HH:mm:ss")).Append("'")
                            .Append(" and alarmid='").Append(keytimes[i].Alarmid[j]).Append("'");// and AlarmActive='0'");
                        nextDatarow = dt.Select(sbSelect.ToString());
                        if (preDatarow.Length > 0 && preDatarow[0]["AlarmActive"].ToString()=="1") dr.Add(preDatarow[0]);
                        dr.AddRange(currentDatarow);
                        if (nextDatarow.Length > 0 && nextDatarow[0]["AlarmActive"].ToString()=="0") dr.Add(nextDatarow[0]);
                    }
                }
            }


            if (dr.Count > 0)
            {
                dt = dr.ToArray().CopyToDataTable().DefaultView.ToTable(true);
                dr.Clear();
                dr.AddRange(dt.Select());
                //显示报警内容
                for (int i = dr.Count - 1 ; i >= 0 ; i--)
                {
                    if (dr[i]["AlarmActive"].ToString() == "0")
                    {
                        lst_Alarm.Items.Add(new ListViewItem(new string[] { (lst_Alarm.Items.Count + 1).ToString() , dr[i]["AlarmName"].ToString() , dr[i]["AlarmLevel"].ToString() , "" , dr[i]["SaveTime"].ToString() }));//最后是报警等级，最后修改
                    }
                    else
                    {
                        //1这里加载数据，对数据进行重组
                        int lastindex = -10;
                        for (int k = lst_Alarm.Items.Count - 1 ; k >= 0 ; k--)
                        {
                            if (dr[i]["AlarmName"].ToString() == lst_Alarm.Items[k].SubItems[1].Text && lst_Alarm.Items[k].SubItems[3].Text == "")
                            {
                                lst_Alarm.Items[k].SubItems[3].Text = dr[i]["SaveTime"].ToString();
                                lastindex = k;
                                break;
                            }
                        }
                        if (lastindex == -10)
                        {
                            lst_Alarm.Items.Add(new ListViewItem(new string[] { (lst_Alarm.Items.Count + 1).ToString() , dr[i]["AlarmName"].ToString() , dr[i]["AlarmLevel"].ToString() , dr[i]["SaveTime"].ToString() , "" }));
                        }
                    }
                }
                lbl_Title.Text = "该料框相关报警信息如下";
            }
            else
                lbl_Title.Text = "该料框无相关报警信息！";
            //对各个分区进行遍历并检验对应报警是否和本postion相关。

        }
        private void LoadAlarmInterface()
        {
            lst_Alarm.Clear();
            lst_Alarm.Location = new Point(2 , 30);
            lst_Alarm.Columns.Add(his_AlarmTitle[0] , 100 , HorizontalAlignment.Left);
            lst_Alarm.Columns.Add(his_AlarmTitle[1] , lst_Alarm.Width - 622 , HorizontalAlignment.Center);
            lst_Alarm.Columns.Add(his_AlarmTitle[2] , 100 , HorizontalAlignment.Left);
            lst_Alarm.Columns.Add(his_AlarmTitle[3] , 200 , HorizontalAlignment.Left);
            lst_Alarm.Columns.Add(his_AlarmTitle[4] , 200 , HorizontalAlignment.Left);
        }

        private void Form_QueryAlarmByBox_Load(object sender , EventArgs e)
        {
            ScreenSize = Screen.PrimaryScreen.WorkingArea.Size;
            lst_Alarm.Size = new Size(ScreenSize.Width , ScreenSize.Height - 60);
            ShowBoxAlarm();
        }
    }
}
